﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Models.WebClient;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceWarn
    {

        EFYihongIotContext ef = new EFYihongIotContext();

        //public iot_device_linkage_warn getWarn(string l_guid)
        //{
        //    return ef.iot_device_linkage_warn.Where(c => c.w_l_guid == l_guid).FirstOrDefault();

        //}
        public string getProductWarn(string pguid, int languagecode)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            //读取语言

            bs_language language = eFIotBase.bs_language.Where(c => c.l_code == languagecode).FirstOrDefault();

            List<bs_language_iot_product_item> languageproducts = (from p in eFIotBase.bs_language_iot_product
                                                                   join i in eFIotBase.bs_language_iot_product_item
                                                                   on p.lp_guid equals i.lpi_lp_guid
                                                                   where p.lp_p_guid == pguid && i.lpi_l_guid == language.l_guid
                                                                   select i).ToList();


            string sql = @"select distinct wl_key,wl_l_guid from iot_device_linkage_warn inner join  iot_device_linkage_warn_log a  on wl_l_guid=w_l_guid
inner join iot_device on d_guid=wl_d_guid and wl_key!='status'
where w_status=1 and w_warn_status=1 and d_p_guid='" + pguid + "'";
            DataTable dt = MySqlYihongFileServer.GetDataTable(sql);

            List<string> titles = new List<string>();
            string res = "";
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["wl_key"].ToString();
                name = cnchangeLanguage(name, languageproducts, dr["wl_l_guid"].ToString());
                if (titles.Where(c => c == name).FirstOrDefault() == null)
                {
                    titles.Add(name);

                    res = res + name + "，";
                }
            }
            res = res.Trim('，');

            if (res != "")
            {
                bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == 1048).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_ci_content + "：" + res;
                }
            }
            else
            {
                bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == 1049).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_ci_content;
                }
            }
            return res;
        }
        public string cnchangeLanguage(string value, List<bs_language_iot_product_item> languageproducts, string lguid)
        {
            int luangaueCode = 0;
            if (value.ToLower() == "ec")
            {
                return "EC";
            }
            else if (value.ToLower() == "ph")
            {
                return "PH";
            }
            else if (value.ToLower() == "temperature" && (lguid == "69eb5225-8c0a-4c43-a8a2-5b371659c0a2" || lguid == "b1aa3699-4af8-495c-be8f-67390141a7e3"))
            {
                luangaueCode = 1030;
            }
            else if (value.ToLower() == "humidity")
            {
                luangaueCode = 1031;
            }
            else if (value.ToLower() == "light")
            {
                luangaueCode = 1038;
            }
            else if (value.ToLower() == "temperature" && (lguid == "f67eed79-e1cb-463f-9d39-d89e679af7c2" || lguid == "6fe267c8-c2a9-4109-8d84-e5ad175e5705"))
            {
                luangaueCode = 1043;
            }
            else if (value.ToLower() == "co2")
            {
                luangaueCode = 1026;
            }

            bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == luangaueCode).FirstOrDefault();
            if (lanuage != null)
            {
                value = lanuage.lpi_ci_content;
            }

            return value;
        }
        //独立语言库
        public string getProductWarnAlone(string pguid, int languagecode)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            //读取语言

            bs_language language = eFIotBase.bs_language.Where(c => c.l_code == languagecode).FirstOrDefault();
            ViewDeviceCurrentValue viewDeviceCurrentValue = new ViewDeviceCurrentValue();
            string sql = "select  d_p_model_no,vc_key,vc_value from iot_device_value_current c inner join iot_device on d_guid=vc_d_guid  where d_p_guid='" + pguid + "'";
            sql = sql + " and d_p_model_no in(35,36,37,38,39,40,41)";
            DataTable dt = MySqlYihongFileServer.GetDataTable(sql);
            List<string> titles = new List<string>();
            string res = "";
            foreach (DataRow dr in dt.Rows)
            {
                viewDeviceCurrentValue = new ViewDeviceCurrentValue();
                viewDeviceCurrentValue.modelNo = int.Parse(dr["d_p_model_no"].ToString());
                viewDeviceCurrentValue.key = dr["vc_key"].ToString();
                viewDeviceCurrentValue.value = dr["vc_value"].ToString();


                if (dr["d_p_model_no"].ToString() == "35" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟EC值是否报警
                    res = res + cnchangeLanguageAlone("ec", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "36" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟PH值是否报警
                    res = res + cnchangeLanguageAlone("ph", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "37" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟水温值是否报警
                    res = res + cnchangeLanguageAlone("temperature", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "38" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟光强度值是否报警
                    res = res + cnchangeLanguageAlone("light", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "39" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟温度值是否报警
                    res = res + cnchangeLanguageAlone("temperature", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "40" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟湿度值是否报警
                    res = res + cnchangeLanguageAlone("humidity", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "41" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟二氧化碳值是否报警
                    res = res + cnchangeLanguageAlone("co2", viewDeviceCurrentValue.modelNo, pguid, language.l_guid) + ",";
                }
            }

            res = res.Trim(',');
            if (res != "")
            {
                bs_language_product_item_alone lanuage = (from p in eFIotBase.bs_language_product_alone
                                                          join i in eFIotBase.bs_language_product_item_alone
                                                        on p.lp_guid equals i.lpi_lp_guid
                                                          where p.lp_p_guid == pguid && i.lpi_l_guid == language.l_guid && p.lp_code == 1048
                                                          select i).FirstOrDefault();// languageproducts.Where(c => c.lpi_c_code == 1048).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_content + "：" + res;
                }
            }
            else
            {
                bs_language_product_item_alone lanuage = (from p in eFIotBase.bs_language_product_alone
                                                          join i in eFIotBase.bs_language_product_item_alone
                                                        on p.lp_guid equals i.lpi_lp_guid
                                                          where p.lp_p_guid == pguid && i.lpi_l_guid == language.l_guid && p.lp_code == 1049
                                                          select i).FirstOrDefault();// languageproducts.Where(c => c.lpi_c_code == 1049).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_content;
                }
            }
            return res;
        }
        public string cnchangeLanguageAlone(string value, int modelNo, string pguid, string languageGuid)
        {
            int luangaueCode = 0;
            if (value.ToLower() == "ec")
            {
                return "EC";
            }
            else if (value.ToLower() == "ph")
            {
                return "PH";
            }
            else if (value.ToLower() == "temperature" && modelNo == 39)
            {
                luangaueCode = 1030;
            }
            else if (value.ToLower() == "humidity")
            {
                luangaueCode = 1031;
            }
            else if (value.ToLower() == "light")
            {
                luangaueCode = 1038;
            }
            else if (value.ToLower() == "temperature" && modelNo == 37)
            {
                luangaueCode = 1043;
            }
            else if (value.ToLower() == "co2")
            {
                luangaueCode = 1026;
            }
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            bs_language_product_item_alone lanuage = (from p in eFIotBase.bs_language_product_alone
                                                      join i in eFIotBase.bs_language_product_item_alone
                                                    on p.lp_guid equals i.lpi_lp_guid
                                                      where p.lp_p_guid == pguid && i.lpi_l_guid == languageGuid && p.lp_code == luangaueCode
                                                      select i).FirstOrDefault();
            // bs_language_product_item_alone lanuage = languageproducts.Where(c => c.lpi_c_code == luangaueCode).FirstOrDefault();
            if (lanuage != null)
            {
                value = lanuage.lpi_content;
            }

            return value;
        }

        public string getProductWarn2(string pguid, int languagecode)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            //读取语言

            bs_language language = eFIotBase.bs_language.Where(c => c.l_code == languagecode).FirstOrDefault();

            List<bs_language_iot_product_item> languageproducts = (from p in eFIotBase.bs_language_iot_product
                                                                   join i in eFIotBase.bs_language_iot_product_item
                                                                   on p.lp_guid equals i.lpi_lp_guid
                                                                   where p.lp_p_guid == pguid && i.lpi_l_guid == language.l_guid
                                                                   select i).ToList();


            ViewDeviceCurrentValue viewDeviceCurrentValue = new ViewDeviceCurrentValue();

            string sql = "select  d_p_model_no,vc_key,vc_value from iot_device_value_current c inner join iot_device on d_guid=vc_d_guid  where d_p_guid='" + pguid + "'";

            sql = sql + " and d_p_model_no in(35,36,37,38,39,40,41)";


            DataTable dt = MySqlYihongFileServer.GetDataTable(sql);


            List<string> titles = new List<string>();
            string res = "";
            foreach (DataRow dr in dt.Rows)
            {
                viewDeviceCurrentValue = new ViewDeviceCurrentValue();
                viewDeviceCurrentValue.modelNo = int.Parse(dr["d_p_model_no"].ToString());
                viewDeviceCurrentValue.key = dr["vc_key"].ToString();
                viewDeviceCurrentValue.value = dr["vc_value"].ToString();


                if (dr["d_p_model_no"].ToString() == "35" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟EC值是否报警
                    res = res + cnchangeLanguageV2("ec", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "36" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟PH值是否报警
                    res = res + cnchangeLanguageV2("ph", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "37" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟水温值是否报警
                    res = res + cnchangeLanguageV2("temperature", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "38" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟光强度值是否报警
                    res = res + cnchangeLanguageV2("light", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "39" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟温度值是否报警
                    res = res + cnchangeLanguageV2("temperature", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "40" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟湿度值是否报警
                    res = res + cnchangeLanguageV2("humidity", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
                else if (dr["d_p_model_no"].ToString() == "41" && dr["vc_value"].ToString() == "yes")
                {
                    //虚拟二氧化碳值是否报警
                    res = res + cnchangeLanguageV2("co2", viewDeviceCurrentValue.modelNo, languageproducts) + ",";
                }
            }

            res = res.Trim(',');
            if (res != "")
            {
                bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == 1048).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_ci_content + "：" + res;
                }
            }
            else
            {
                bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == 1049).FirstOrDefault();
                if (lanuage != null)
                {
                    res = lanuage.lpi_ci_content;
                }
            }
            return res;
        }

        public string cnchangeLanguageV2(string value, int modelNo, List<bs_language_iot_product_item> languageproducts)
        {
            int luangaueCode = 0;
            if (value.ToLower() == "ec")
            {
                return "EC";
            }
            else if (value.ToLower() == "ph")
            {
                return "PH";
            }
            else if (value.ToLower() == "temperature" && modelNo == 39)
            {
                luangaueCode = 1030;
            }
            else if (value.ToLower() == "humidity")
            {
                luangaueCode = 1031;
            }
            else if (value.ToLower() == "light")
            {
                luangaueCode = 1038;
            }
            else if (value.ToLower() == "temperature" && modelNo == 37)
            {
                luangaueCode = 1043;
            }
            else if (value.ToLower() == "co2")
            {
                luangaueCode = 1026;
            }

            bs_language_iot_product_item lanuage = languageproducts.Where(c => c.lpi_c_code == luangaueCode).FirstOrDefault();
            if (lanuage != null)
            {
                value = lanuage.lpi_ci_content;
            }

            return value;
        }



        public ModelPages getWarnLog(string p_guid, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            string sql = "select  d_name,wl_warn_status,wl_add_time,wl_value,d_guid,wl_id from iot_warn_log l inner join iot_device on d_guid=wl_d_guid where d_p_guid='" + p_guid+"' " ;
            DataTable dt = MySqlYihongFileServer.getPageDataTable(pageIndex, pageSize, sql, sugarParameters, "wl_id desc", ref totalRecord);
           

            ModelPages modelPages = new ModelPages();

            modelPages.data = dt;
            modelPages.totalRecord = totalRecord;
            modelPages.pageSize = pageSize;
            modelPages.pageCount = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;//分的页数
            modelPages.pageIndex = pageIndex;
            return modelPages;
        }
    }
}
